package org.jingle.sketch.cursor
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	import org.jingle.sketch.component.Composite;
	import org.jingle.sketch.component.Shape;
	
	public class CursorManager1
	{
		private var _cursor:Sprite;
		private static var CURSOR:String = "customize.cursor";
		private static var instance:CursorManager1 = new CursorManager1();		
		function CursorManager1()
		{
		}
		
		public static function getInstance():CursorManager1 {
			return instance;
		} 

		public function setCursor(cursor:Sprite, container:Shape, eventType:String):void {
			var old:DisplayObject = container.sprite.getChildByName(CURSOR);
			if(old){
				container.sprite.removeChild(old);
			}			
			cursor.name = CURSOR;
			container.sprite.addChild(cursor);
			container.sprite.addEventListener(eventType, showCursor);
			
			Mouse.hide();
		}
		
		public function clearCursorIfLeave(container:Shape):void {
			container.sprite.stage.addEventListener(Event.MOUSE_LEAVE, hideCursor);
		}
		
		private function hideCursor(evt:Event):void {
			var target:Object = evt.target;
			if(target is Sprite){
				var sprite:Sprite = target as Sprite;
				var cursor:Sprite = sprite.getChildByName(CURSOR) as Sprite;
				if(cursor){
//					cursor.visible = false;
//					Mouse.show();
				}else{
					cursor.visible = false;
					Mouse.show();
				}
			}
		}
		
		private function showCursor(evt:Event):void {
			var target:Object = evt.target;
			if(target is Sprite){
				var sprite:Sprite = target as Sprite;
				var cursor:Sprite = sprite.getChildByName(CURSOR) as Sprite;
				if(cursor){
					if (!cursor.visible){
						cursor.visible = true;
						//Mouse.hide();
					} 
					
					cursor.x = sprite.mouseX;
					cursor.y = sprite.mouseY;
					//evt.updateAfterEvent();
				}
			}
		} 
	}
}